/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package jdklearning.encrypt;

//import com.llq.commons.utils.verify.tools.Assert;

import java.math.BigInteger;
import java.security.MessageDigest;
//import org.apache.commons.lang.StringUtils;

public class DigestTest {
	public static final String MD_5 = "md5";
	public static final String SEP = "-";
	public static final String HTTP = "http://";
	private static String UTF_8 = "utf-8";

	private static String md5(String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes(UTF_8));
		} catch (Throwable e) {
			throw new RuntimeException("Md5 message exception.");
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);
		for (int i = 0; i < 32 - md5code.length(); ++i) {
			md5code = "0" + md5code;
		}
		return md5code;
	}

	public static String generateAuthCdnImgUrl(String domain, String filename, String params, String authKey,
			long timestamp) {
//		Assert.isNotBlank("域名不能为空", domain);
//		Assert.isNotBlank("文件名不能为空", filename);
//		Assert.isNotBlank("验证key不能为空", authKey);
//		Assert.isTrue("时间戳必须大于0", timestamp > 0L);

		if (!(filename.startsWith("/"))) {
			filename = "/" + filename;

//			if (StringUtils.isNotBlank(params)) {
//				filename = filename + "@" + params;
//			}
		}

		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(filename).append("-").append(String.valueOf(timestamp).substring(0, 10)).append("-")
				.append(0).append("-").append(0).append("-").append(authKey);

		String md5 = md5(stringBuilder.toString());
		stringBuilder = stringBuilder.delete(0, stringBuilder.length());
		stringBuilder.append(domain).append(filename);

		StringBuilder append = stringBuilder.append("?").append("auth_key=")
				.append(String.valueOf(timestamp).substring(0, 10)).append("-").append(0).append("-").append(0)
				.append("-").append(md5);

		return "http://" + append.toString();
	}

	public static void main(String[] args) {
//		String dev007123sfera = generateAuthCdnImgUrl("img001-tmp.007fenqi.com",
//				"user/201512051402070000026943hand_identity_no681902", null, "FSl87sdfLDF6SFLpF1eF",
//				System.currentTimeMillis() + 30000L);
		
		String dev007123sfera = generateAuthCdnImgUrl("img001-tmp.007fenqi.com",
		"advertisement/201710121852520000000001advertisement005174", null, "FSl87sdfLDF6SFLpF1eF",
		System.currentTimeMillis() + 30000L);

		System.out.println(dev007123sfera);
	}
}